package com.china08.yxyapi.api.manage.sch.platform.weikecheng;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.es.ElasticYxyResResource;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.service.yxweike.YxyWeikeService;

/**
 * 学校管理员---平台管理---微课管理---审核未通过微课
 * 
 */
@CrossOrigin
@RestController
public class WeikechengSrhNoPassController extends YxyApiBaseController {

    @Autowired
    private YxyWeikeService weikeService;

    @RequestMapping(value = "/api_yw/manage/sch/platform/weikecheng/nopass", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel,
            RespModel respModel) {
        String schoolId = getProfile(request, response).getSchoolId();

        // 排序
        Pageable pageable = reqModel.getPageable(Direction.DESC, "uploadTime");
        // 审核未通过微课 # 1
        Page<ElasticYxyResResource> page = weikeService.findResourceSchUp(reqModel.stage, reqModel.subject, schoolId, 2,
                reqModel.keyword.toLowerCase(), pageable);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<RespModel> list = new ArrayList<RespModel>();
        for (ElasticYxyResResource resResource : page) {
            respModel = new RespModel();
            respModel.weikeId = resResource.getId();
            respModel.title = resResource.getTitle();
            // 分类
            List<String> categoryList = new ArrayList<String>();
            String category = "";
            if (StringUtils.isNotBlank(resResource.getStageName())) {
                categoryList.add(resResource.getStageName());
            }

            if (StringUtils.isNotBlank(resResource.getWeikeSubjectName())) {
                categoryList.add(resResource.getWeikeSubjectName());
            }

            if (CollectionUtils.isNotEmpty(resResource.getKnowNames())) {
                categoryList.addAll(resResource.getKnowNames());
            }
            category = StringUtils.join(categoryList, ",");
            respModel.category = category;
            respModel.uploaderName = resResource.getUploaderName();
            respModel.uploaderSchoolName = resResource.getUploaderSchoolName();
            respModel.uploadTime = sdf.format(resResource.getUploadTime());
            respModel.auditUserName = resResource.getAuditUserName();
            respModel.auditSchoolName = resResource.getAuditSchoolName();
            respModel.auditTime = sdf.format(resResource.getLastModifiedDate());
            respModel.status = resResource.getStatus();
            respModel.remark = resResource.getRemark();
            list.add(respModel);
        }
        Page<RespModel> pageImpl = new PageImpl<RespModel>(list, pageable, page.getTotalElements());
        return pageImpl;
    }

    protected static class ReqModel extends YxyApiPageReqModel {

        public String stage;// 学段

        public String subject;// 学科

        public String keyword;// 上传者,微课名称
    }

    protected static class RespModel extends YxyApiRespModel {

        public String weikeId;// 微课id

        public String title;// 标题

        public String category;// 分类

        public String uploaderName;// 上传者名称

        public String uploaderSchoolName;// 上传者所属学校

        public String uploadTime;// 上传时间

        public String auditUserName;// 审核人名称

        public String auditSchoolName;// 审核人所属学校

        public String auditTime;// 审核时间

        public Integer status;// 微课状态 都是2 其实

        public String remark; // 原因

    }
}
